package com.gelsanalyzer.business;

import java.util.*;

import javax.swing.table.*;

public class CourseEntryList extends AbstractTableModel {
    private List<CourseEntryItem> courseList;
    private int count;
    private String[] columns={"Department", "Course", "Goal(s)" };
    
    public CourseEntryList() {
        count=0;
        courseList = new ArrayList<CourseEntryItem>();
    }
    
    public int getIndexof(CourseEntryItem CEI) {
        for(int i=0; i<count; i++) {
            if( CEI.getCourse().getCourseId() == courseList.get(i).
                    getCourse().getCourseId() )
                return i;            
        }
        return -1; //not found
    }
    
    public void addCourse(Course course) {
        if(course != null) {
           CourseEntryItem cei = new CourseEntryItem(course, course.getDepartment()); 
           addCourseEntryItem(cei);
        }
    }   
    
    public CourseEntryItem removeCourse(Course course) {
        if(course != null) {
        CourseEntryItem cei = new CourseEntryItem(course, course.getDepartment());
        int position = getIndexof(cei);
        courseList.remove(position);
        count--;        
        fireTableDataChanged();        
        return cei;        
        }
        return null;
    }
    
  
    
    public void addCourseEntryItem(CourseEntryItem cei) {
        courseList.add(cei);
        count++;
        fireTableDataChanged();
    }
    
    public List<CourseEntryItem> getCourseList() {
        return courseList;
    }
    
    public void setCourseList(List<CourseEntryItem> courseList) {
        this.courseList = courseList;
    }
    
    public int getCount() {
        return count;
    }
    
    public String getColumnName(int col) {
        return columns[col];
      }

    
    public int getColumnCount() {
        return columns.length;
    }

    
    public int getRowCount() {
        return courseList.size();
    }

    
    public Object getValueAt(int row, int col) {
        CourseEntryItem CEI = courseList.get(row);
        
       
        StringBuilder sb = new StringBuilder();
        
        for(Goalarea ga : CEI.getGoalarea()) {
            sb.append(ga);
            sb.append(" ");
        }
        
        switch(col) {
            case 0:
                return CEI.getDepartment().toString();
            case 1:
                return CEI.getCourse().toString();
            case 2:
                return sb.toString();
            default:
                return null;
        }
    }
}
